import datetime
import os.path
import sys
from fileinput import filename

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager


#创建一个浏览器对象
class Driver:
    driver = ""
    #构造函数
    def __init__(self):
        options = webdriver.ChromeOptions()
        self.driver = webdriver.Chrome(service = Service(ChromeDriverManager().install()),options = options)

    #创建屏幕截图的方法
    def getScreeShot(self):
        #图片文件名称：被执行的方法名+2025-05-25-172621.png（年月日时分秒）
        #图片路径：(根据每天的日期进行分类)../images/2025-05-25/2025-05-25-172621.png
        #注意，该方法不能用于截图有弹窗的界面
        dirname = datetime.datetime.now().strftime('%Y-%m-%d')
        #../images/2025-05-25
        if not os.path.exists('../images/' + dirname):
            os.mkdir('../images/' + dirname)
        #../images/2025-05-25/LoginFailTest-2025-05-25-172621.png
        filename = sys._getframe().f_back.f_code.co_name+"-"+datetime.datetime.now().strftime('%Y-%m-%d-%H%M%S')+".png"
        self.driver.save_screenshot("../images/"+dirname+"/"+filename)

BlogDriver = Driver()